from hashlib import sha1
import hmac
import time


# 获取13位的时间戳
def now_to_timestamp(digits=13):
    time_stamp = int(round(time.time() * 1000))
    return time_stamp


# 时间戳转换回固定格式的date
def timestamp_to_date(time_change):
    time_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(time_change) / 1000))
    return time_date


# 加密签名
def get_signal(time_stamp):
    a = hmac.new(bytes("d1b964811afb40118a12068ff74a12f4", "utf-8"), digestmod=sha1)  # HMAC key
    a.update(bytes("password", "utf-8"))  # 固定字符串
    a.update(bytes("c3cef7c66a1843f8b3a9e6a1e3160e20", "utf-8"))  # clienId
    a.update(bytes("com.zhihu.web", "utf-8"))  # 固定字符串
    a.update(bytes(time_stamp, "utf-8"))  # timeStamp
    return a.hexdigest()


# print(get_signal(str(now_to_timestamp())))
